package com.zaofan.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import com.zaofan.bean.Account;
import com.zaofan.bean.AccountLog;
import com.zaofan.bean.Recharge;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.RechargeDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.PayBean;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.PayUtils;
import com.zaofan.utils.SqlBuilderUtils;
import com.zaofan.utils.WebUtils;

public class RechargeService {
	private RechargeDao rechargetDao = new RechargeDao();
	private AccountService accountService = new AccountService();

	public void delete(Recharge recharget) {
		rechargetDao.del(recharget);
	}

	public void add(Recharge recharget) {
		rechargetDao.add(recharget);
	}

	public void update(Recharge recharget) {
		rechargetDao.update(recharget);
	}

	public List<Recharge> findAll(Map<String, Object> params) {
		List<Recharge> data = rechargetDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return rechargetDao.count(params);
	}

	public Recharge findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return rechargetDao.findOne(params);
	}

	public PayBean recharge(Integer accountId, BigDecimal money)
			throws ServiceException {
		PayBean payBean = new PayBean();
		Account account = accountService.findById(accountId);
		if (account == null) {
			throw new ServiceException("账户信息有误!!");
		}
		if (money.compareTo(new BigDecimal(5)) < 0) {
			throw new ServiceException("对不起,平台最低充值5元!!");
		}
		Recharge recharge = new Recharge();
		recharge.setAccountId(accountId);
		recharge.setAccountMobile(account.getMobile());
		recharge.setAddTime(new Date());
		recharge.setMoney(money);
		recharge.setPayState(StateConstant.ORDER_PAY_STATE_UN_PAY);
		recharge.setState(StateConstant.ORDER_STATE_ORDER);
		recharge.setRechargeNo(WebUtils.getOrderId(accountId));
		add(recharge);
		payBean.setTotalPrice(money);
		payBean.setOrderNo(recharge.getRechargeNo());
		payBean.setFreePrice(new BigDecimal(0));
		payBean.setAlipay(PayUtils.getAlipayRechargeString(
				recharge.getRechargeNo(), "早范结算", "早范-充值", money));
		payBean.setWeiPay(PayUtils.getWeiPayRecharge(payBean.getOrderNo(),
				"早范", "早范充值", money));
		return payBean;
	}

	public Recharge findByRechargeNo(String out_trade_no) {
		Map<String, Object> params = new HashMap<>();
		params.put("rechargeNo", out_trade_no);
		return rechargetDao.findOne(params);
	}

	public List<Recharge> findByAccountId(Integer accountId) {
		Map<String, Object> params = new HashMap<>();
		params.put("accountId", accountId);
		params.put("state", StateConstant.ORDER_STATE_PAY);
		params.put("payState", StateConstant.ORDER_PAY_STATE_PAY);
		return findAll(params);

	}

	public void payByAdmin(String rechargeNo) throws ServiceException {
		Recharge recharge = this.findByRechargeNo(rechargeNo);
		if (recharge == null) {
			throw new ServiceException("订单号有误!!");
		}
		Account account = accountService.findById(recharge.getAccountId());
		Connection connection = C3p0DataSourceUtils.getConnection();
		try {
			connection.setAutoCommit(false);
			// 更新订单状态
			String sql = "update "
					+ Recharge.class.getSimpleName().toLowerCase()
					+ " set payState = " + StateConstant.ORDER_PAY_STATE_PAY
					+ ",state = " + StateConstant.ORDER_STATE_PAY
					+ ",payType='管理员支付操作' where rechargeNo = '" + rechargeNo
					+ "'";
			QueryRunner runner = new QueryRunner();
			runner.update(connection, sql);
			
			AccountLog log = new AccountLog();
			log.setAccountId(account.getId());
			log.setAccountMobile(account.getMobile());
			log.setAddTime(new Date());
			log.setAfterMoney(account.getMoney().add(recharge.getMoney()));
			log.setBeforeMoney(account.getMoney());
			log.setMoney(recharge.getMoney());
			log.setType("用户在线充值,管理手动补充");
			sql = SqlBuilderUtils.builderInsertSql(AccountLog.class, log);
			runner.update(connection, sql);
			account.setMoney(account.getMoney().add(recharge.getMoney()));
			sql = SqlBuilderUtils.builderUpdateSql(Account.class, account);
			runner.update(connection, sql);
			connection.commit();
		} catch (Exception e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}